﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VietThinks.Security;
using System.Web.Mvc;
using Infrastructure;
namespace VietThinks.Website
{
    public partial class VisitContext
    {
        public interface ISessionConditionAttribute
        {
        }

        public class KeepAfterLoginAttribute : Attribute, ISessionConditionAttribute
        {
        }


        // TODO: review variables' neccessity
        public class SessionManager
        {
            private SessionManager() { }

            private static readonly SessionManager instance = new SessionManager();

            internal static SessionManager Instance
            {
                get { return instance; }
            }

            public void ClearAll()
            {
                HttpContext.Current.Session.RemoveAll();
            }

            public void ClearWithConditions<T>()
                where T : ISessionConditionAttribute
            {
                foreach (var proper in this.GetType().GetProperties())
                {
                    var customerAttribute = proper.GetCustomAttributes(typeof(T), true);
                    if (customerAttribute.IsEmpty())
                    {
                        proper.SetValue(this, null, null);
                    }
                }
            }

            public void Abandon()
            {
                HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", null));

                HttpContext.Current.Session.Abandon();
            }

            public IUserPrincipal Visitor
            {
                get { return (IUserPrincipal)HttpContext.Current.Session["Visitor"]; }
                set { HttpContext.Current.Session["Visitor"] = value; }
            }

            public IUserPrincipal Staff
            {
                get { return (IUserPrincipal)HttpContext.Current.Session["Staff"]; }
                set { HttpContext.Current.Session["Staff"] = value; }
            }

            public string CustomerVertical
            {
                get { return (string)HttpContext.Current.Session["CustomerVertical"]; }
                set { HttpContext.Current.Session["CustomerVertical"] = value; }
            }

            public string CustomerType
            {
                get { return (string)HttpContext.Current.Session["CustomerType"]; }
                set { HttpContext.Current.Session["CustomerType"] = value; }
            }

            public string CurrentOmniturePageName
            {
                get { return (string)HttpContext.Current.Session["CurrentOmniturePageName"]; }
                set { HttpContext.Current.Session["CurrentOmniturePageName"] = value; }
            }

            //[KeepAfterLoginAttribute()]
            //public CartManager CartManager
            //{
            //    get { return (CartManager)HttpContext.Current.Session["Cart"]; }
            //    set { HttpContext.Current.Session["Cart"] = value; }
            //}

            //public CheckoutProcess CheckoutProcess
            //{
            //    get { return (CheckoutProcess)HttpContext.Current.Session["CheckoutProcess"]; }
            //    set { HttpContext.Current.Session["CheckoutProcess"] = value; }
            //}

            public string ZipCode
            {
                get { return (string)HttpContext.Current.Session["ZipCode"]; }
                set { HttpContext.Current.Session["ZipCode"] = value; }
            }

            //public IList<SearchResultFilterViewModel> SelectedFilters
            //{
            //    get { return (IList<SearchResultFilterViewModel>)HttpContext.Current.Session["SelectedFilters"]; }
            //    set { HttpContext.Current.Session["SelectedFilters"] = value; }
            //}

            public string FilterSource
            {
                get { return (string)HttpContext.Current.Session["FilterSource"]; }
                set { HttpContext.Current.Session["FilterSource"] = value; }
            }

            //public EmailThisPageViewModel EmailThisPage
            //{
            //    get { return (EmailThisPageViewModel)HttpContext.Current.Session["EmailThisPage"]; }
            //    set { HttpContext.Current.Session["EmailThisPage"] = value; }
            //}

            public string SubCatCode
            {
                get { return (string)HttpContext.Current.Session["SubCatCode"]; }
                set { HttpContext.Current.Session["SubCatCode"] = value; }
            }

            public string CatOrBrandCode
            {
                get { return (string)HttpContext.Current.Session["CatOrBrandCode"]; }
                set { HttpContext.Current.Session["CatOrBrandCode"] = value; }
            }

            private bool IsInitChannelAdvisor
            {
                get { return (bool)(HttpContext.Current.Session["IsInitChannelAdvisor"] ?? false); }
                set { HttpContext.Current.Session["IsInitChannelAdvisor"] = value; }
            }

            public string OmnitureSessionId
            {
                get { return (string)HttpContext.Current.Session["OmnitureSessionId"]; }
                set { HttpContext.Current.Session["OmnitureSessionId"] = value; }
            }

            public string OmnitureTransactionId
            {
                get { return (string)HttpContext.Current.Session["OmnitureTransactionId"]; }
                set { HttpContext.Current.Session["OmnitureTransactionId"] = value; }
            }

            public MvcHtmlString RegisterChannelAdvisor()
            {
                if (!IsInitChannelAdvisor)
                {
                    IsInitChannelAdvisor = true;
                    return MvcHtmlString.Create(@"
                        <script type='text/javascript'>
                            <!--
                            {
                                document.write('<img src=https://tracking.searchmarketing.com/welcome.asp?SMCID=32001257&x=' + escape(document.referrer) + ' width=1 height=1>');
                            } //--> 
                        </script>");
                }
                return MvcHtmlString.Empty;
            }
        }
    }
}